﻿using System;
using System.Diagnostics;
using System.IO;

using Microsoft.SharePoint;

namespace Datagility.Shpt.SPDocMirror.Service
{
    /// <summary>
    /// Provides access to a SharePoint Document Library (or sub-folder) so that Documents can be read and
    ///  added to 'shadowed' folders on the file system.
    /// </summary>
    internal class DocLibrary: IDisposable
    {
        SPSite siteCollection = null;
        SPFolder folder = null;
        string url;

        public string Url
        {
            get { return url; }
            set { url = value; }
        }

        /// <summary>
        /// Parses the DocFolder string to get a reference to the appropriate Document Library folder. When this value is based
        ///  upon a <see cref="Datagility.Shpt.DocMirror.AuditEntry.DocFolder"/> property the constructor determines where in 
        ///  the string the site names end and where the library and folder names begin.
        /// </summary>
        /// <param name="SiteCollection">A reference to the root Site Collection</param>
        /// <param name="DocFolder">A string representing the path to a Document Folder. 
        /// E.g. subsite1/Shared Documents/folder1</param>
        public DocLibrary(SPSite SiteCollection, Guid WebId, Guid ListId)
        {
            SPMirrorLoggingService.Write("Information", String.Format("DocLibrary.ctor(SPSite, string) Starting... - SiteCollection = {0}, WebId = {1}, ListId = {2} ",
                SiteCollection.Url, WebId, ListId));

            if (SiteCollection == null)
            {
                throw new ArgumentException("Parameter Cannot be Null", "SiteCollection");
            }
            if (WebId == null)
            {
                throw new ArgumentException("Parameter Cannot be Null", "WebId");
            }
            if (ListId == null)
            {
                throw new ArgumentException("Parameter Cannot be Null", "ListId");
            }

            siteCollection = SiteCollection;
            using (SPWeb web = siteCollection.OpenWeb(WebId))
            {
                folder = web.Lists[ListId].RootFolder;
                url = folder.ServerRelativeUrl;
            }
            SPMirrorLoggingService.Write("Information", String.Format("DocLibrary.ctor(SPSite, string) Complete - Folder = {0}",
                folder.Name));
        }

        /// <summary>
        /// Reads a Document from a Document Library or sub-folder. No exception is raised is the document is not found.
        /// </summary>
        /// <param name="DocName">The file name of the Document</param>
        /// <returns>The document as a byte array</returns>
        public byte[] GetDocument(string DocName)
        {
            SPMirrorLoggingService.Write("Information", String.Format("DocLibrary.GetDocument Starting... DocName = {0}", 
                DocName));

            byte[] doc = null;
            int docLength = 0;
            try
            {
                SPFile file = folder.Files[DocName];
                doc = file.OpenBinary();
                docLength = doc.Length;
            }

            //In the case of renaming a document an update is issued against the old
            // document name.
            catch (System.ArgumentException)
            {
            }

            SPMirrorLoggingService.Write("Information", String.Format("DocLibrary.GetDocument Complete - doc Size = {0}",
                docLength.ToString()));

            return doc;
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            if (siteCollection != null)
            {
                siteCollection.Dispose();
            }
        }
    }
}
